package org.blueoxygen.kukuruyuk.actions.supplier;
import java.lang.reflect.InvocationTargetException;
import java.sql.Timestamp;
import org.apache.commons.beanutils.PropertyUtils;
import org.blueoxygen.cimande.LogInformation;
import org.blueoxygen.kukuruyuk.entity.Supplier;
public class ChangeStatus extends SupplierForm {
	
public String execute(){
	    if(getSupplier().getId() != null && !"".equalsIgnoreCase(getSupplier().getId())){
	        	setSupplier((Supplier) manager.getById(Supplier.class, getSupplier().getId()));
		}
		
		if(hasErrors()){
			return INPUT;
		}

		LogInformation log;
		if(getSupplier().getId() == null || "".equalsIgnoreCase(getSupplier().getId())){
			log = new LogInformation();
			log.setCreateBy(sessionCredentials.getCurrentUser().getId());
			log.setCreateDate(new Timestamp(System.currentTimeMillis()));
			getSupplier().setId(null);
		} else {
			Supplier temp = getSupplier();
			setSupplier((Supplier) manager.getById(Supplier.class, getSupplier().getId()));
			log = getSupplier().getLogInformation();
			try {
				PropertyUtils.copyProperties(getSupplier(), temp);
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			}
		}
		
		
		log.setLastUpdateBy(sessionCredentials.getCurrentUser().getId());
		log.setLastUpdateDate(new Timestamp(System.currentTimeMillis()));
		
		if (getSupplier().getLogInformation().getActiveFlag()== 1){
		log.setActiveFlag(9);
		}
		
		
		getSupplier().setLogInformation(log);
	
		manager.save(getSupplier());
		return SUCCESS;
	}

	
}	
